//
//  KSCheckLogisticsViewController.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/27.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

class KSCheckLogisticsViewController: KSBaseViewController {

    var tableview: UITableView!
    var consultArray: [KSOrderRefundDetailDataLogsModel] = []
    var logistArray: [KSLogisticDetailDataDataModel] = []
    var num: String = ""
    var logisticId: String = ""
    var isConsult: Bool = true
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = KSColor.kColorViewBackground
        self.title = isConsult ? "协商历史" : "物流详情"
        tableview = UITableView()
        tableview.register(KSCheckLogisticsHeadTableViewCell.self, forCellReuseIdentifier: "KSCheckLogisticsHeadTableViewCell")
        tableview.register(KSCheckLogisticsDetailTableViewCell.self, forCellReuseIdentifier: "KSCheckLogisticsDetailTableViewCell")
        tableview.register(KSChecklogisticsCallTableViewCell.self, forCellReuseIdentifier: "KSChecklogisticsCallTableViewCell")
        tableview.backgroundColor = KSColor.kColorViewBackground
        tableview.separatorStyle = .none
        tableview.delegate = self
        tableview.dataSource = self
        self.view.addSubview(tableview)
        tableview.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0))
        }
        
        if !isConsult {
            self.requestLogistcDetail()
        }
    }
    
    private func requestLogistcDetail() {
        self.request.requestLogisticDetail(num: num, kuaidiId: logisticId)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
    }
    
    override func requestFaile(code: Int, message: String, apiType: KSApiType) {
        super.requestFaile(code: code, message: message, apiType: apiType)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension KSCheckLogisticsViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var num = 0
        if consultArray.count > 0 || logistArray.count > 0 {
            if isConsult {
                num = consultArray.count + 2
            } else {
                num = logistArray.count + 2
            }
        }
        return num
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var cellHeight: CGFloat = 0
        if indexPath.row == 0 {
            cellHeight = 100
        } else if indexPath.row == consultArray.count + 1 {
            cellHeight = 50
        } else {
            cellHeight = KSTools.getHeightByWidth(KSConstants.kScreenWidth - 56, title: "汪汪汪汪汪无无无无汪汪汪汪汪无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无无", size: 15) + 40
        }
        return cellHeight
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var num = logistArray.count + 2
        if isConsult {
            num = consultArray.count + 2
        }
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSCheckLogisticsHeadTableViewCell", for: indexPath)
            return cell
        } else if indexPath.row == num + 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSChecklogisticsCallTableViewCell", for: indexPath)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSCheckLogisticsDetailTableViewCell", for: indexPath) as! KSCheckLogisticsDetailTableViewCell
            if indexPath.row == 1 {
                cell.setProgressState(isLastRow: false,isFirst: true)
            } else if indexPath.row == num {
                cell.setProgressState(isLastRow: true,isFirst: false)
            }
            
            if isConsult {
                cell.setDataWithConsultModel(consultModel: consultArray[indexPath.row - 2])
            } else {
                cell.setDataWithLogisticModel(logisticModel: logistArray[indexPath.row - 2])
            }
            return cell
        }
    }
}
